package com.example.yifei.ui.page.main.home


import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import com.example.yifei.R
import com.example.yifei.ui.page.common.RouteName
import com.example.yifei.utils.AppUserUtil

/**
 * @Author: zhudaihao
 * @Date: 2022/5/7
 * @Describe:
 */

@Composable
fun HomePage(
    navCtrl: NavHostController,
    viewModel: HomeViewModel = hiltViewModel()
) {
    Log.e("TAG", "------- HomePage ")

    Column(
        modifier = Modifier
            .background(color = Color(0xffF4F6FB))
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        Box {
            Image(
                painter = painterResource(R.mipmap.top_bg),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(190.dp)
            )

            Title()


            Column(
                modifier = Modifier
                    .padding(start = 16.dp, top = 178.dp, end = 16.dp)
            ) {
                Column(
                    modifier = Modifier
                        .background(
                            shape = RoundedCornerShape(4.dp),
                            color = Color.White
                        )
                        .fillMaxWidth()
                ) {
                    LazyColumn {
                        itemsIndexed(viewModel.list) { index, item ->
                            Item(index, item, onClick = {

                                //生成需求
                                if (index == 0) {
                                    AppUserUtil.demandIndex = 1
                                    navCtrl.navigate(RouteName.PRODUCTION_DEMAND) {
                                        popUpTo(navCtrl.graph.findStartDestination().id) {
                                            saveState = true
                                        }
                                        launchSingleTop = true
                                        restoreState = true
                                    }

                                }

                                //采购列表
                                if (index == 1) {
                                    AppUserUtil.listIndex = 2
                                    navCtrl.navigate(RouteName.PURCHASE_LIST) {
                                        popUpTo(navCtrl.graph.findStartDestination().id) {
                                            saveState = true
                                        }
                                        launchSingleTop = true
                                        restoreState = true
                                    }

                                }

                                if (index == 2) {
                                    AppUserUtil.listIndex = 3
                                    navCtrl.navigate(RouteName.PURCHASE_LIST) {
                                        popUpTo(navCtrl.graph.findStartDestination().id) {
                                            saveState = true
                                        }
                                        launchSingleTop = true
                                        restoreState = true
                                    }

                                }


                                if (index == 3) {
                                    AppUserUtil.listIndex = 5
                                    navCtrl.navigate(RouteName.PURCHASE_LIST) {
                                        popUpTo(navCtrl.graph.findStartDestination().id) {
                                            saveState = true
                                        }
                                        launchSingleTop = true
                                        restoreState = true
                                    }

                                }

                            })
                        }
                    }


                }

            }


        }

    }
}

@Composable
private fun Title() {
    Column(
        modifier = Modifier
            .padding(start = 16.dp, top = 88.dp, end = 16.dp)
    ) {
        Text(
            modifier = Modifier.padding(start = 2.dp),
            text = "名创优品饰品贸易有…",
            fontSize = 20.sp,
            color = Color(0xff373C55)
        )

        Row(
            modifier = Modifier.padding(top = 24.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {

            Image(
                painter = painterResource(id = R.mipmap.icon_daiban),
                contentDescription = null,
            )

            Text(
                modifier = Modifier.padding(start = 2.dp),
                text = "待处理事项",
                fontSize = 16.sp,
                color = Color(0xff373C55)
            )
        }
    }
}

@Composable
private fun Item(index: Int, item: HomeViewItemBean, onClick: () -> Unit) {
    var height: Dp = 0.dp
    if (index != 0) {
        height = 0.5.dp
    }

    Box(
        modifier = Modifier
            .height(height)
            .fillMaxWidth()
            .padding(start = 16.dp, end = 16.dp)
            .background(color = Color(0xffF0F0F0))
    )

    Column(modifier = Modifier
        .clickable { onClick() }) {
        Row(
            modifier = Modifier
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Text(
                text = item.name,
                fontSize = 14.sp,
                color = Color(0xff373C55)
            )

            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {
                Text(
                    modifier = Modifier.padding(start = 5.dp),
                    text = "(${item.state})",
                    fontSize = 12.sp,
                    color = Color(0xff8D90A8)
                )


                Text(
                    modifier = Modifier.padding(start = 2.dp),
                    text = item.number,
                    fontSize = 14.sp,
                    color = Color(0xffFF6666)
                )
            }


        }


    }
}
 